/*     */ package com.zimbra.cs.imap;
/*     */ 
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.util.ByteUtil;
/*     */ import com.zimbra.cs.store.Blob;
/*     */ import com.zimbra.cs.store.BlobBuilder;
/*     */ import com.zimbra.cs.store.StoreManager;
/*     */ import java.io.ByteArrayInputStream;
/*     */ import java.io.DataInputStream;
/*     */ import java.io.EOFException;
/*     */ import java.io.IOException;
/*     */ import java.io.InputStream;
/*     */ import java.nio.ByteBuffer;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ abstract class Literal
/*     */ {
/*     */   public static Literal newInstance(int size)
/*     */     throws IOException
/*     */   {
/*  34 */     return newInstance(size, false);
/*     */   }
/*     */   
/*     */ 
/*  38 */   public static Literal newInstance(int size, boolean useBlob) throws IOException { return (useBlob) && (size > 0) ? new BlobLiteral(size) : new ByteBufferLiteral(size); }
/*     */   
/*     */   public abstract int size();
/*     */   
/*     */   public abstract int remaining();
/*     */   
/*     */   public abstract int copy(InputStream paramInputStream) throws IOException;
/*     */   
/*     */   public abstract int put(byte[] paramArrayOfByte, int paramInt1, int paramInt2) throws IOException;
/*     */   public abstract InputStream getInputStream() throws IOException;
/*     */   public abstract byte[] getBytes() throws IOException;
/*     */   public abstract Blob getBlob() throws IOException, ServiceException;
/*     */   public abstract void cleanup();
/*  51 */   public int put(ByteBuffer bb) throws IOException { assert (bb.hasArray());
/*  52 */     int len = put(bb.array(), bb.arrayOffset() + bb.position(), bb.remaining());
/*  53 */     bb.position(bb.position() + len);
/*  54 */     return len;
/*     */   }
/*     */   
/*     */   protected void checkComplete() {
/*  58 */     if (remaining() > 0) {
/*  59 */       throw new IllegalStateException("Incomplete literal");
/*     */     }
/*     */   }
/*     */   
/*     */   private static class ByteBufferLiteral extends Literal {
/*     */     private ByteBuffer buf;
/*     */     private Blob blob;
/*     */     
/*     */     ByteBufferLiteral(int size) {
/*  68 */       this.buf = ByteBuffer.allocate(size);
/*     */     }
/*     */     
/*     */     public int size() {
/*  72 */       return this.buf.capacity();
/*     */     }
/*     */     
/*     */     public int remaining() {
/*  76 */       return this.buf.capacity() - this.buf.position();
/*     */     }
/*     */     
/*     */     public byte[] getBytes() {
/*  80 */       checkComplete();
/*  81 */       return this.buf.array();
/*     */     }
/*     */     
/*     */     public InputStream getInputStream() {
/*  85 */       checkComplete();
/*  86 */       return new ByteArrayInputStream(this.buf.array());
/*     */     }
/*     */     
/*     */     public int copy(InputStream is) throws IOException {
/*  90 */       int count = is.read(this.buf.array(), this.buf.position(), remaining());
/*  91 */       if (count > 0) {
/*  92 */         this.buf.position(this.buf.position() + count);
/*     */       }
/*  94 */       return count;
/*     */     }
/*     */     
/*     */     public int put(byte[] b, int off, int len) {
/*  98 */       if (len > remaining()) {
/*  99 */         len = remaining();
/*     */       }
/* 101 */       this.buf.put(b, off, len);
/* 102 */       return len;
/*     */     }
/*     */     
/*     */     public Blob getBlob() throws IOException, ServiceException {
/* 106 */       return StoreManager.getInstance().storeIncoming(getInputStream());
/*     */     }
/*     */     
/*     */     public void cleanup() {
/* 110 */       this.buf = null;
/* 111 */       if (this.blob != null) {
/* 112 */         StoreManager.getInstance().quietDelete(this.blob);
/* 113 */         this.blob = null;
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   private static class BlobLiteral extends Literal {
/*     */     private BlobBuilder builder;
/*     */     int size;
/*     */     
/*     */     BlobLiteral(int size) throws IOException {
/*     */       try {
/* 124 */         this.size = size;
/* 125 */         this.builder = StoreManager.getInstance().getBlobBuilder();
/* 126 */         this.builder.init();
/*     */       } catch (ServiceException e) {
/* 128 */         throw error("Unable to initialize BlobBuilder", e);
/*     */       }
/*     */     }
/*     */     
/*     */     public int size() {
/* 133 */       return this.size;
/*     */     }
/*     */     
/*     */     public int remaining() {
/* 137 */       return size() - (int)this.builder.getTotalBytes();
/*     */     }
/*     */     
/*     */     public byte[] getBytes() throws IOException {
/* 141 */       DataInputStream is = new DataInputStream(getInputStream());
/*     */       try {
/* 143 */         byte[] b = new byte[size()];
/* 144 */         is.readFully(b);
/* 145 */         assert (is.read() == -1);
/* 146 */         return b;
/*     */       } finally {
/* 148 */         ByteUtil.closeStream(is);
/*     */       }
/*     */     }
/*     */     
/*     */     public InputStream getInputStream() throws IOException {
/* 153 */       return getBlob().getInputStream();
/*     */     }
/*     */     
/*     */     public int put(byte[] b, int off, int len) throws IOException {
/* 157 */       if (len > remaining()) {
/* 158 */         len = remaining();
/*     */       }
/* 160 */       if (len > 0) {
/* 161 */         this.builder.append(b, off, len);
/*     */       }
/* 163 */       return len;
/*     */     }
/*     */     
/*     */     public int copy(InputStream is) throws IOException {
/* 167 */       int count = 0;
/* 168 */       if (remaining() > 0) {
/* 169 */         byte[] b = new byte[' '];
/*     */         do {
/* 171 */           int len = is.read(b, 0, Math.min(b.length, remaining()));
/* 172 */           if (len < 0) {
/* 173 */             throw new EOFException("Unexpected end of stream");
/*     */           }
/* 175 */           count += put(b, 0, len);
/* 176 */         } while ((remaining() > 0) && (is.available() > 0));
/*     */       }
/* 178 */       return count;
/*     */     }
/*     */     
/*     */     public Blob getBlob() throws IOException {
/* 182 */       checkComplete();
/*     */       try {
/* 184 */         return this.builder.finish();
/*     */       } catch (ServiceException e) {
/* 186 */         throw error("Unable to finalize BlobBuilder", e);
/*     */       }
/*     */     }
/*     */     
/*     */     public void cleanup() {
/* 191 */       if (this.builder != null) {
/* 192 */         this.builder.dispose();
/* 193 */         this.builder = null;
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   static IOException error(String msg, Throwable cause) {
/* 199 */     IOException e = new IOException(msg);
/* 200 */     e.initCause(cause);
/* 201 */     return e;
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/imap/Literal.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */